<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>新建计划</title>
    <link rel="stylesheet" href="../../lib/layui/layui/css/layui.css">
    <link rel="stylesheet" href="../../lib/layui/css/layui.css">
    <script type="text/javascript" src="../../js/news/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../../lib/layui/layui/lay/dest/layui.all.js"></script>
    <script src="../../js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../lib/layui/layui/layui.js"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery-1.8.3.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.ui.widget.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.iframe-transport.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.fileupload.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/ajaxupload.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jquery.form.min.js"></script>
    <script type="text/javascript" src="/js/common/fileupload.js"></script>
    <script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <style>
        * {
            font-family: "Microsoft Yahei" !important;
        }

        body {
            overflow-y: hidden;
        }

        nav button {
            margin-left: 5px;
            padding: .25rem .5rem;
            font-size: .875rem;
            border-radius: .2rem;
            float: right !important;
            margin-top: 10px
        }

        nav div {
            float: left !important;
            margin: 15px;
        }

        nav {
            height: 50px;
            border-bottom: 1px solid #cfdbe2;
            background-color: #fafbfc;
            border-radius: 0;
        }

        .layui-tab {
            margin: 0;
        }

        .content {
            height: 544px;
            overflow: auto;
            margin: 14px;
            padding: 14px;
            background: #fff;
        }

        .layui-tab-content {
            background-color: #F5F7FA;
        }

        .search {
            display: inline;
            margin-bottom: 1px;
            padding-top: 6px;
            padding-bottom: 6px;
            width: 250px;
            border: 0;
        }

    </style>
</head>
<body>
<form class="layui-form form" action="">
    <nav>
        <div class="title">新建计划</div>
        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm return" style="margin-right: 30px">返回
        </button>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm addBtn">保存
        </button>
    </nav>
    <div>
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <div class="layui-tab-content ">
                <div class="content">
                    <div class="layui-form-item">
                        <label class="layui-form-label">计划编号<b style="color: red">*</b></label>
                        <div class="layui-input-block layui-input-inline">

                            <input type="text" name="planCode" lay-verify="title" autocomplete="off" placeholder=""
                                   class="layui-input required">
                        </div>
                        <label class="layui-form-label">计划名称<b style="color: red">*</b></label>
                        <div class="layui-input-block layui-input-inline">
                            <input type="text" name="planName" lay-verify="title" autocomplete="off" placeholder=""
                                   class="layui-input required">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">计划类别<b style="color: red">*</b></label>
                        <div class="layui-input-inline" style="margin-left: 110px">
                            <select id="planType">

                            </select>
                        </div>
                        <div>
                            <label class="layui-form-label">计划周期<b style="color: red">*</b></label>
                            <div class="layui-input-block layui-input-inline">
                                <input style="width: 100px" type="text" name="planCycle" lay-verify="title"
                                       autocomplete="off" placeholder="" class="layui-input required">
                            </div>
                            <div class="layui-input-inline" style="width: 60px; margin-left:-70px">
                                <select class="planCycleUnit">
                                    <option value="1">天</option>
                                    <option value="2">周</option>
                                    <option value="3">月</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div>
                        <div class="layui-form-item">
                           <label class="layui-form-label">支出预算 <b style="color: red">*</b></label>
                            <div class="layui-input-block layui-input-inline">

                                <input type="text" name="expenseBudget" lay-verify="title" autocomplete="off"
                                       placeholder="" class="layui-input required">
                            </div>
                            <label class="layui-form-label">收入预估 <b style="color: red">*</b></label>
                            <div class="layui-input-block layui-input-inline">
                                <input type="text" name="incomeBudget" lay-verify="title" autocomplete="off"
                                       placeholder="" class="layui-input required">
                            </div>
                        </div>
                    </div>
                    <div>
                        <div class="layui-form-item">
                            <label class="layui-form-label "></label>
                            <div class="layui-input-block layui-input-inline">

                                <input type="checkbox" name="isStart" title="支出超预算开启" lay-skin="primary">
                            </div>
                            <label class="layui-form-label "></label>
                            <div class="layui-input-block layui-input-inline">
                                <input type="checkbox" name="planStatus" title="立即启动方案" lay-skin="primary">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">计划描述</label>
                        <div class="layui-input-block">
                            <textarea id="reasons" class="planDesc" style="display: none;"></textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">相关附件</label>
                            <div class="layui-input-block" style="width: 190px"
                                 style="position: relative;margin-left: 0px;">
                                <button id="uploadimg" type="button" class="layui-btn"><i
                                        class="layui-icon">&#xe67c;</i>上传附件
                                </button>
                                <div id="Attachment" style="padding-bottom: 10px"></div>
                                <input type="hidden" name="attachmentId" autocomplete="off" class="layui-input">
                                <input type="hidden" name="attachmentName" autocomplete="off"
                                       class="layui-input">

                                <input type="file" multiple="multiple" name="file" id="uploadinputimg"
                                       class="w-icon5"
                                       style="position: absolute;opacity: 0;width: 95px;top: 0px;left:0px;height:40px;filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0)">
                            </div>
                        </div>
                    </div>
                    <div>
                        <div class="layui-form-item">
                            <label class="layui-form-label ">查看范围</label>
                            <div class="layui-input-block ">
                                <input type="checkbox" name="allUser" lay-filter="allUser" title="全体成员"
                                       lay-skin="primary">
                            </div>
                            <div class="user" style="display: none">
                                <div class="layui-input-block ">
                                    <label class="">用户</label>
                                    <div class="layui-input-inline;border-bottom: 1px solid #ccc"
                                         style="display:inline">
                                        <input type="text" style="border-bottom: 1px solid #ccc;" class="search parent"
                                               placeholder="搜索" id="query_userId" disabled/>
                                        <span style="padding: 5px;color: #999;cursor: pointer;margin-left:-26px"><i
                                                class="layui-icon layui-icon-user query_adduser"></i></span>
                                    </div>
                                </div>
                                <div class="layui-input-block ">
                                    <label class="">部门</label>
                                    <div class="layui-input-inline;border-bottom: 1px solid #ccc"
                                         style="display:inline">
                                        <input disabled type="text" style="border-bottom: 1px solid #ccc;"
                                               class="search parent"
                                               placeholder="搜索" id="DEPT_NAME" deptid="" name="DEPT_NAME"/>
                                        <span style="padding: 5px;color: #999;cursor: pointer;margin-left:-26px"><i
                                                class="layui-icon layui-icon-group dept_add"></i></span>
                                    </div>
                                </div>
                                <div class="layui-input-block ">
                                    <label class="">角色</label>
                                    <div class="layui-input-inline;border-bottom: 1px solid #ccc"
                                         style="display:inline">
                                        <input disabled type="text" style="border-bottom: 1px solid #ccc;"
                                               class="search parent"
                                               placeholder="搜索" id="USER_PRIV_NAME"/>
                                        <span style="padding: 5px;color: #999;cursor: pointer;margin-left:-26px"><i
                                                class="layui-icon layui-icon-username userPriv_add"></i></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
        </div>
    </div>
</form>
</body>
<script>
    $('.content').height($(window).height());
    $('.classBtn').click(function () {
        $(this).toggleClass("one")
        $(this).siblings('button').removeClass("one");
    });
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);

        if (r != null) {
            return unescape(r[2]);
        } else {
            return null;
        }
    }
    $('#uploadinputimg').click(function () {
        $('.form').attr('action', '/upload?module=revenue')
        fileuploadFn('#uploadinputimg', $('#Attachment'));
    })
    $('.return').click(function () {
        history.go(-1)
    })
    //附件删除
    $('#Attachment').on('click', '.deImgs', function () {
//        var data=$(this).parents('.dech').attr('deUrl');
        var dome = $(this).parents('.dech');
        layer.confirm('确定要删除吗？', {
            btn: ['确定', '取消'], //按钮
            title: "删除附件"
        }, function () {
            dome.remove();
            layer.msg('删除成功', {icon: 6});
            layer.closeAll();
        })
//        deleteChatment(data,dome);
    })
    layui.use(['form', 'layedit', 'laydate', 'layedit', 'table'], function () {
        var form = layui.form
            , layer = layui.layer
            , laydate = layui.laydate
            , layedit = layui.layedit
            , table = layui.table
            , upload = layui.upload;

        var reasons = layedit.build('reasons'); //建立编辑器
        form.render();
        //选部门
        $('.dept_add').click(function () {
            dept_id = 'DEPT_NAME';
            $.popWindow("../../common/selectDept");
        });
        //选角色
        $('.userPriv_add').click(function () {
            priv_id = 'USER_PRIV_NAME';
            $.popWindow("../../common/selectPriv");
        });
        //选人员
        $(".query_adduser").on("click", function () {
            user_id = 'query_userId';
            $.popWindow("../../common/selectUser");
        });
        $.ajax({
            url: '/IncomeType/findIncomType',
            type: 'get',
            dataType: 'json',
            success: function (data) {
                var str = '<option>请选择计划类别</option>';
                for (var i = 0; i < data.obj.length; i++) {
                    str += '<option value="' + data.obj[i].planTypeId + '">' + data.obj[i].planTypeName + '</option>'
                }
                $('#planType').append(str);
                $('.layui-unselect .layui-form-select .layui-form-selected').css("overflow", "visible")
                form.render('select');
            }
        });
        if (GetQueryString('planId') != '' && GetQueryString('planId') != null) {
            $('.title').text('编辑计划')
            $.ajax({
                url: '/IncomePlan/findIncomePlanById',
                type: 'get',
                dataType: 'json',
                data: {
                    planId: GetQueryString('planId')
                },
                success: function (data) {
                    var data = data.object
                    $('[name="planCode"]').val(data.planCode)
                    $('[name="planName"]').val(data.planName)
                    $('[name="planCycle"]').val(data.planCycle)
                    $('[name="expenseBudget"]').val(data.expenseBudget)
                    $('[name="incomeBudget"]').val(data.incomeBudget)
                    $('[name="planStatus"]').val(data.planStatus)
                    var planType = 'dd[lay-value=' + data.planTypeId + ']';
                    $('#planType').siblings("div .layui-form-select").find('dl').find(planType).click()
                    var planCycleUnit = 'dd[lay-value=' + data.planCycleUnit + ']';
                    $('.planCycleUnit').siblings("div .layui-form-select").find('dl').find(planCycleUnit).click()

                    if (data.isStart == 0) {
                        //不开启
                        $('[name="isStart"]').prop("checked", false);
                    } else if (data.isStart == 1) {
                        //开启
                        $('[name="isStart"]').prop("checked", true);
                    }
                    if (data.planStatus == 2) {
                        //不开启
                        $('[name="planStatus"]').prop("checked", false);
                    } else if (data.planStatus == 1) {
                        //开启
                        $('[name="planStatus"]').prop("checked", true);
                    }
                    //用户
                    if (data.allUser == "1") {
                        $('.user').hide()
                        $('[name="allUser"]').prop("checked", true);
                    } else {
                        $('.user').show()
                        $('#USER_PRIV_NAME').val(data.deptNames);//选角色
                        $('#USER_PRIV_NAME').attr('userpriv', data.roleIds);
                        $('#query_userId').val(data.userNames);//选人
                        $('#query_userId').attr('user_id', data.userIds);
                        $('#DEPT_NAME').val(data.deptNames);//选部门
                        $('#DEPT_NAME').attr('deptid', data.deptIds);
                        $('[name="allUser"]').prop("checked", false);
                    }
                    var str = ''
                    if(data.map.AttachmentList!='' && data.map.AttachmentList!= undefined){
                        for(var i=0;i<data.map.AttachmentList.length;i++){
                            if (data.map.AttachmentList[i] != '' && data.map.AttachmentList[i] != undefined) {
                                str += '<div class="dech" deUrl="' + data.map.AttachmentList[i].attUrl + '"><a href="/download?' + data.map.AttachmentList[i].attUrl + '" NAME="' + data.map.AttachmentList[i].attachName + '*"><img style="width:16px;" src="/img/file/cabinet@.png"/>' + data.map.AttachmentList[i].attachName + '</a><img class="deImgs" style="margin-left:5px;cursor: pointer;" src="/img/file/icon_deletecha_03.png"/><input type="hidden" class="inHidden" value="' + data.map.AttachmentList[i].aid + '@' + data.map.AttachmentList[i].ym + '_' + data.map.AttachmentList[i].attachId + ',"></div>';
                            } else {
                                str = '';
                            }
                        }
                    }

                    $('#Attachment').html(str)
                    layedit.setContent(reasons, data.planDesc);
                    form.render()

                }
            });
        }
        function empty(num) {
            if(num==undefined||num==null){
                return ''
            }else{
                return num
            }
        }

        $('.addBtn').click(function () {
            var allUser;
            if ($('[name="allUser"]').attr('checked') == 'checked') {
                allUser = 1
            } else {
                allUser = 0
            }
            var isStart;
            if ($('[name="isStart"]').attr('checked') == 'checked') {
                isStart = 1
            } else {
                isStart = 0
            }
            var planStatus;
            if ($('[name="planStatus"]').attr('checked') == 'checked') {
                planStatus = 1
            } else {
                planStatus = 2
            }
            //获取附件id和name
            var attachmentId = '';
            var attachmentName = '';
            for (var i = 0; i < $('#Attachment .inHidden').length; i++) {
                attachmentId += $('#Attachment .inHidden').eq(i).val();
            }
            for (var i = 0; i < $('#Attachment .inHidden').length; i++) {
                attachmentName += $('#Attachment a').eq(i).attr('NAME');
            }

            for(var i=0;i<$('.required').length;i++){
                if($('.required').eq(i).val()==''){
                    $.layerMsg({content:''+$('b').eq(i).parent().text().split('*')[0]+'不能为空',icon:2});
                    return false;
                }
            }
            if($('#planType').val()=='请选择计划类别'){
                $.layerMsg({content:'计划类别不能为空',icon:2});
            }else{
                var data = {
                    planId: empty(GetQueryString('planId')),
                    planCode: $('[name="planCode"]').val(),
                    planName: $('[name="planName"]').val(),
                    planTypeId:$('#planType').val(),
                    planCycle: $('[name="planCycle"]').val(),
                    planCycleUnit: $('.planCycleUnit').val(),
                    expenseBudget: $('[name="expenseBudget"]').val(),
                    incomeBudget: $('[name="incomeBudget"]').val(),
                    isStart: isStart,
                    planStatus: planStatus,
                    planDesc: layedit.getText(reasons),
                    allUser: allUser,
                    deptIds: empty($('#DEPT_NAME').attr('deptid')),
                    roleIds: empty($('#USER_PRIV_NAME').attr('privid')),
                    userIds: empty($('#query_userId').attr('user_id')),
                    attachmentId: attachmentId,
                    attachmentName: attachmentName,
                };
                $.ajax({
                    url: '/IncomePlan/editOrInsertPlan',
                    type: 'post',
                    dataType: 'json',
                    data: data,
                    success: function (data) {
                        layer.msg('保存成功！')
                        history.go(-1)
                    }
                });
            }
        })
        form.on('checkbox(allUser)', function (data) {
            if (data.elem.checked == true) {
                $('.user').hide()
                $('.user').attr('allUser', 1)
            } else {
                $('.user').show()
                $('.user').remove('allUser')
            }

        });
    });

</script>

</html>
